package org.helgoboss.quick_launcher_ref_impl

import org.helgoboss.osgi_additions._
import org.helgoboss.hotkey_listener.HotkeyListener
import org.helgoboss.commandlet.ExecutionContext
import org.helgoboss.quick_launcher.QuickLauncher
import org.helgoboss.launchlet.{LaunchletExecutionContext, Launchlet}
import collection.JavaConversions

class Activator extends OsgiContext with OsgiConsumer with OsgiProvider with HotkeyListener {
    // TODO Don't use synchronized but use actors
    whenBundleActive {
        val quickLauncherView = new QuickLauncherView {
            def findLaunchlets(searchString: String) = services[Launchlet].filter(_.name.toLowerCase.contains(searchString.toLowerCase))
        }
        
        new QuickLauncher {
            def open(executionContext: ExecutionContext) = {
                quickLauncherView.currentExecutionContext = new LaunchletExecutionContext {
                    import JavaConversions._
                    val arguments = executionContext.arguments
                    // TODO set to commandlet execution context environment as soon as available
                    val environment = System.getenv.toMap
                    // TODO set to commandlet execution context input stream as soon as processable
                    val inputStream = None
                    // TODO set to commandlet execution context outpu stream as soon as processable
                    val outputStream = None
                    val workingDirectory = Some(executionContext.workingDirectory)
                }
                quickLauncherView.makeSureIsInFront
                // TODO Pause calling thread and then pass exit code on
                0
            }
        }.providesService[QuickLauncher]
        
        (Win + Q) triggers {
            quickLauncherView.toggleVisibility
        }
        
        onStop {
            quickLauncherView.dispose
        }
    }
}